// 商品安全库存设置界面
Ext.define("PSI.Goods.SafetyInventoryEditForm", {
    extend: "Ext.window.Window",
    config: {
        parentForm: null,
        entity: null
    },
    initComponent: function () {
        var me = this;
        var entity = me.getEntity();

        Ext.apply(me, {title: "设置供应商",
            modal: true,
            onEsc: Ext.emptyFn,
            width: 580,
            height: 400,
            layout: "border",
            items: [{
                    region: "center",
                    border: 0,
                    bodyPadding: 10,
                    layout: "fit",
                    items: [me.getGoodsGrid()]
                },
                {
                    region: "north",
                    border: 0,
                    layout: {
                        type: "table",
                        columns: 2
                    },
                    height: 40,
                    bodyPadding: 10,
                    items: [
                        {
                            xtype: "hidden",
                            id: "hiddenId",
                            name: "id",
                            value: entity.get("id")
                        },
                        {
                            id: "editRef",
                            fieldLabel: "商品",
                            labelWidth: 60,
                            labelAlign: "right",
                            labelSeparator: ":",
                            xtype: "displayfield",
                            value: entity.get("code") + " " + entity.get("name") + " " + entity.get("spec")
                        }
                    ]
                }],
                buttons: [
                    {
                        text:"新增供应商",
                        iconCls:"PSI-button-add",
                        handler:function () {
                            me.onAddSupplier();
                        },
                        scope:me,
                        id:"buttonAdd"
                    },
                    {
                        text: "保存",
                        iconCls: "PSI-button-ok",
                        handler: me.onOK,
                        scope: me,
                        id: "buttonSave"
                    },
                    {
                        text: "取消",
                        handler: function () {
                    	    PSI.MsgBox.confirm("请确认是否取消当前操作？", function(){
                    		    me.close();
                    	    });
                        },
                        scope: me,
                        id: "buttonCancel"
                    }
                ],
            listeners: {
                show: {
                    fn: me.onWndShow,
                    scope: me
                }
            }
        });

        me.callParent(arguments);
    },
    
    onWndShow: function () {
        var me = this;
        
        var el = me.getEl() || Ext.getBody();
        el.mask(PSI.Const.LOADING);
        Ext.Ajax.request({
            url: PSI.Const.BASE_URL + "Home/Goods/goodsSafetyInventoryList",
            params: {
                id: Ext.getCmp("hiddenId").getValue()
            },
            method: "POST",
            callback: function (options, success, response) {
                el.unmask();

                if (success) {
                    var data = Ext.JSON.decode(response.responseText);

                    var store = me.getGoodsGrid().getStore();
                    store.removeAll();
                    store.add(data);
                } else {
                    PSI.MsgBox.showInfo("网络错误")
                }
            }
        });
    },
    onAddSupplier: function () {
        var me=this;
        var grid=me.__goodsGrid;
        Ext.define('Record', {
            extend: 'Ext.data.Model',
            fields: ["goodsname", "warehouseCode", "warehouseName", "safetyInventory", "unitName",
                "inventoryUpper"]
        });
        var record  = new Record({
            goodsname: me.getEntity().get("name")
        });
        grid.getStore().insert(0, record );

    },
    onOK: function () {
        var me = this;
        Ext.getBody().mask("正在保存中...");
        Ext.Ajax.request({
            url: PSI.Const.BASE_URL + "Home/Goods/editSafetyInventory",
            method: "POST",
            params: {jsonStr: me.getSaveData()},
            callback: function (options, success, response) {
                Ext.getBody().unmask();

                if (success) {
                    var data = Ext.JSON.decode(response.responseText);
                    if (data.success) {
                        PSI.MsgBox.showInfo("成功保存数据", function () {
                            me.close();
                            me.getParentForm().onGoodsSelect();
                        });
                    } else {
                        PSI.MsgBox.showInfo(data.msg);
                    }
                }
            }
        });
    },

    getGoodsGrid: function () {
        var me = this;
        if (me.__goodsGrid) {
            return me.__goodsGrid;
        }
        var modelName = "PSIGoodsSafetyInventory_EditForm";
        Ext.define(modelName, {
            extend: "Ext.data.Model",
            fields: ["goodsname", "suppliername", "warehouseName", "safetyInventory", "unitName",
                     "inventoryUpper"]
        });
        var store = Ext.create("Ext.data.Store", {
            autoLoad: false,
            model: modelName,
            data: []
        });

        me.__cellEditing = Ext.create("PSI.UX.CellEditing", {
        	clicksToEdit: 1,
            listeners: {
                edit: {
                    fn: me.cellEditingAfterEdit,
                    scope: me
                }
            }
        });

        Ext.define("PSIGoodsDic", {
            extend: "Ext.data.Model",
            fields: ["id", "name"]
        });

        var xbDicStore = Ext.create("Ext.data.Store", {
            model: "PSIGoodsDic",
            autoLoad: false,
            data: []
        });
        me.xbDicStore = xbDicStore;

        //var xbDicStore=Ext.create('Ext.data.Store',{
        //    fields:['text','value'],
        //    data:[{'text':'男',"value":"1"},{'text':'女',"value":"2"},{'text':'其它',"value":"3"}],
        //    toValue:function(v){
        //        if(v){
        //            var rec=xbDicStore.find('value',v,0,true,true,false);
        //            if(rec>-1){
        //                return xbDicStore.getAt(rec).get('text');
        //            }else{
        //                return "";
        //            }
        //        }
        //    }
        //});
        me.__goodsGrid = Ext.create("Ext.grid.Panel", {
        	viewConfig: {
                enableTextSelection: true
            },
            plugins: [me.__cellEditing],
            columnLines: true,
            columns: [
            {header: "物品名称", dataIndex: "goodsname", width: 100, menuDisabled: true, sortable: false},
            {header: "供应商", dataIndex: "suppliername", width: 120, menuDisabled: true,sortable: false,
                align: "right", xtype: "numbercolumn",format: "0", editor: {xtype: "combo",displayField: 'text',valueField: 'value',store:xbDicStore}}
            ],
            store: store
        });

        return me.__goodsGrid;
    },
    
    cellEditingAfterEdit: function (editor, e) {
    },

    getSaveData: function () {
        var result = {
            id: Ext.getCmp("hiddenId").getValue(),
            items: []
        };

        var store = this.getGoodsGrid().getStore();
        for (var i = 0; i < store.getCount(); i++) {
            var item = store.getAt(i);
            result.items.push({
                warehouseId: item.get("warehouseId"),
                invUpper: item.get("inventoryUpper"),
                si: item.get("safetyInventory")
            });
        }

        return Ext.JSON.encode(result);
    }
});